#include "ssicloud_classic_case.h"

ssicloud::ClassicTypePage::ClassicTypePage()
    : ListObject<ClassicTypePageLNode>(LQ_BASE_URL "/classicCaseType/page"
                           , OBJECT_NAME_ClassicTypePage
                           , NetUrl::NET_HTTP_METHOD_GET)
{
    mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);
}

bool ssicloud::ClassicTypePage::requestAll(const char *name, int pageNum, int pageSize)
{
    if (isLoading()) return true;

    m_name = name ? name : "";
    m_pageNum = SFString::number(pageNum);
    m_pageSize = SFString::number(pageSize);

    if (!m_name.empty()) mUrl.appendChange("name", m_name.c_str());
    mUrl.appendChange("pageNum", m_pageNum.c_str());
    mUrl.appendChange("pageSize", m_pageSize.c_str());

    return obtain();
}

void ssicloud::ClassicTypePage::profile()
{
    ListTable<ClassicTypePageLNode>::iterator it = mNodes.begin();
    GEN_Printf(LOG_INFO, "---------案例分析-获取案例类型-------");

    for (; it != mNodes.end(); ++it)
    {
         GEN_Printf(LOG_DUMP, "ID:%s, 类型名称:%s, 备注:%s, 创建时间:%s, 最后更新时间:%s",
                    it->id.string(), it->name.c_str(), it->remark.c_str(), it->createTime.c_str(), it->updateTime.c_str());
    }
    GEN_Printf(LOG_INFO, "---------完成-------");
}

int ssicloud::ClassicTypePage::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    // GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *dataJson = cJSON_GetObjectItem(root, "data");
    if (dataJson)
    {
        cJSON *records = cJSON_GetObjectItem(dataJson, "records");
        if (records) {
            for (cJSON *item = records->child; NULL != item; item = item->next )
            {
                ClassicTypePageLNode tmp;

                JSON_VALUETRING_SWAP_BYTESTRING(item, id, tmp.id);
                JSON_VALUETRING_SWAP_BYTESTRING(item, createTime, tmp.createTime);
                JSON_VALUETRING_SWAP_BYTESTRING(item, name, tmp.name);
                JSON_VALUETRING_SWAP_BYTESTRING(item, remark, tmp.remark);
                JSON_VALUETRING_SWAP_BYTESTRING(item, updateTime, tmp.updateTime);

                // GEN_Printf(LOG_DEBUG, "items size=%d", tmp.items.size());
                mNodes.push_back(tmp);
            }
        }

        if (mNodes.empty())
        {
            GEN_Printf(LOG_WARN, "load Category is empty.");
            ret = KL_DATA_PRISER_EMPTY;
        } else
        {
            profile();
        }
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}

ssicloud::ClassicCasePage::ClassicCasePage()
    : ListObject<ClassicCasePageLNode>(LQ_BASE_URL "/classicCase/page"
                           , OBJECT_NAME_ClassicCasePage
                           , NetUrl::NET_HTTP_METHOD_GET)
{
    mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);

    mUrl.append("status", "1");
}

bool ssicloud::ClassicCasePage::requestAll(const char *title, int pageNum, int pageSize)
{
    if (isLoading()) return true;

    m_title = title ? title : "";
    m_pageNum = SFString::number(pageNum);
    m_pageSize = SFString::number(pageSize);

    if (!m_title.empty()) mUrl.appendChange("title", m_title.c_str());
    mUrl.appendChange("pageNum", m_pageNum.c_str());
    mUrl.appendChange("pageSize", m_pageSize.c_str());

    return obtain();
}

void ssicloud::ClassicCasePage::profile()
{
    ListTable<ClassicCasePageLNode>::iterator it = mNodes.begin();
    GEN_Printf(LOG_INFO, "---------案例分析-分页查询-------");

    for (; it != mNodes.end(); ++it)
    {
        GEN_Printf(LOG_DUMP, "ID:%s, title:%s, 类型名称:%s, 创建时间:%s, 最后更新时间:%s",
                   it->id.string(), it->title.c_str(), it->typeName.c_str(), it->createTime.c_str(), it->updateTime.c_str());

    }
    GEN_Printf(LOG_INFO, "---------完成-------");
}

int ssicloud::ClassicCasePage::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    // GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *dataJson = cJSON_GetObjectItem(root, "data");
    if (dataJson)
    {
        cJSON *records = cJSON_GetObjectItem(dataJson, "records");
        if (records) {
            for (cJSON *item = records->child; NULL != item; item = item->next )
            {
                ClassicCasePageLNode tmp;

                JSON_VALUETRING_SWAP_BYTESTRING(item, id, tmp.id);
                JSON_VALUETRING_SWAP_BYTESTRING(item, title, tmp.title);
                JSON_VALUETRING_SWAP_BYTESTRING(item, content, tmp.content);
                JSON_VALUETRING_SWAP_BYTESTRING(item, typeName, tmp.typeName);
                JSON_VALUETRING_SWAP_BYTESTRING(item, createTime, tmp.createTime);
                JSON_VALUETRING_SWAP_BYTESTRING(item, updateTime, tmp.updateTime);

                // GEN_Printf(LOG_DEBUG, "items size=%d", tmp.items.size());
                mNodes.push_back(tmp);
            }
        }

        if (mNodes.empty())
        {
            GEN_Printf(LOG_WARN, "load Category is empty.");
            ret = KL_DATA_PRISER_EMPTY;
        } else
        {
            profile();
        }
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}
